package com.pengpeng.sheepfoldmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 请求跨域配置类
 */
@Configuration
public class SheepFoldCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        //创建一个配置信息类
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");  //跨域请求暴露的字段为所有
        corsConfiguration.addAllowedMethod("*");  //允许所有请求方式进行跨域
        corsConfiguration.addAllowedOrigin("*");  //允许所有请求来源进行跨域
        corsConfiguration.setAllowCredentials(true);  //允许携带Cookie进行跨域，防止跨域访问时丢失Cookie信息

        //任意路径都要注册跨域配置
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
